using System.Threading.Tasks;
using Aurora.TaskScheduler.Extension;
using Aurora.TaskScheduler.Abstractions.Message;
using Aurora.TaskScheduler.Abstractions.Storage;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Aurora.TaskScheduler.Dashboard.Areas.MyFeature.Pages
{
    public class MainModel : PageModel
    {
        private ITaskHandler _quartzHandle;
        private IDataStorage _storage;

        public MainModel(ITaskHandler quartzHandle, IDataStorage storage)
        {
            _quartzHandle = quartzHandle;
            _storage = storage;
        }

        [BindProperty] public AppTask Input { get; set; }

        /// <summary>
        /// 获取任务列表
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> OnGetSelectJob()
        {
            var jobs = await _quartzHandle.GetJobs(true);

            return new JsonDataResult(jobs);
        }

        // /// <summary>
        // /// 新建任务
        // /// </summary>
        // /// <returns></returns>
        // public async Task<IActionResult> OnPostAddJob()
        // {
        //    var date = await  _quartzHandle.AddJob(Input);
        //     Input.Status = Convert.ToInt32(JobState.暂停);
        //     return new JsonDataResult(date);
        // }
        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> OnPostPauseJob()
        {
            var data = await _quartzHandle.Pause(Input);

            return new JsonDataResult(DashboardResult.Create(data));
        }

        /// <summary>
        /// 开启任务
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> OnPostStartJob()
        {
            var data = await _quartzHandle.Start(Input);

            return new JsonDataResult(DashboardResult.Create(data));
        }

        /// <summary>
        /// 立即执行任务
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> OnPostRunJob()
        {
            var data = await _quartzHandle.Run(Input);
            return new JsonDataResult(DashboardResult.Create(data));
        }

        /// <summary>
        /// 修改任务
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> OnPostUpdateJob()
        {
            var data = await _quartzHandle.Update(Input);

            return new JsonDataResult(DashboardResult.Create(data));
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> OnPostDeleteJob()
        {
            var data = await _quartzHandle.Remove(Input);

            return new JsonDataResult(DashboardResult.Create(data));
        }

        /// <summary>
        /// 获取任务执行记录
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> OnPostJobRecord(string groupName, string taskName, int current, int size)
        {
            var data = await _storage.GetLogList(groupName, taskName, current, size);

            return new JsonDataResult(DashboardPageResult<AppTaskLog>.Create(data.list, data.total));
        }

        // /// <summary>
        // /// 获取已注入的任务类
        // /// </summary>
        // /// <returns></returns>
        // public IActionResult OnGetSelectClassJob()
        // {
        //     var date = ClassJobsFactory.ClassJobs;
        //
        //     return new JsonDataResult(date);
        // }
        public void OnGet()
        {
        }
    }
}